package dms.presentation.action.teacher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import dms.dto.StudentAffairTeacherDTO;
import dms.model.role.StudentAffairTeacher;
import dms.model.role.wrapper.StudentAffairTeacherWrapper;

import dms.util.Constant;
import dms.util.ContextUtil;

public class InformationAction extends MappingDispatchAction {
	private static final Log log = LogFactory.getLog(InformationAction.class);
	public ActionForward personal(ActionMapping mapping, 
			  ActionForm form,
			  HttpServletRequest request,
			  HttpServletResponse response) throws Exception {
StudentAffairTeacherWrapper wrapper = (StudentAffairTeacherWrapper) ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
StudentAffairTeacher studentAffairTeacher = wrapper.getStudentAffairTeacher();

request.setAttribute("studentAffairTeacher", studentAffairTeacher);
return mapping.findForward(Constant.SUCCESS);
}

/**
* 修改个人信息不包括密码
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward modifyInfo(ActionMapping mapping, 
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {

ActionErrors errors= new ActionErrors();
StudentAffairTeacherDTO dto = (StudentAffairTeacherDTO) form;

StudentAffairTeacherWrapper wrapper = (StudentAffairTeacherWrapper) ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
dto.setId(wrapper.getStudentAffairTeacher().getId());

try {
wrapper.modifyPersonalInfo(dto);
} catch (Exception e) {
this.log.error(e.getMessage());
return mapping.findForward(Constant.ERROR);
}

return mapping.findForward(Constant.SUCCESS);
}

public ActionForward modifyInfoForm(ActionMapping mapping, 
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
	StudentAffairTeacherWrapper wrapper = (StudentAffairTeacherWrapper) ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
	StudentAffairTeacher studentAffairTeacher = wrapper.getStudentAffairTeacher();

request.setAttribute("studentAffairTeacher", studentAffairTeacher);
return mapping.findForward(Constant.SUCCESS);
}

public ActionForward modifyPassword(ActionMapping mapping, 
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {

	StudentAffairTeacherDTO dto = (StudentAffairTeacherDTO) form;
	StudentAffairTeacherWrapper wrapper = (StudentAffairTeacherWrapper) ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
dto.setId(wrapper.getStudentAffairTeacher().getId());
try {
wrapper.modifyPersonalInfo(dto);
} catch (Exception e) {
log.error(e.getMessage());
return mapping.findForward(Constant.ERROR);
}

return mapping.findForward(Constant.SUCCESS);
}

@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return this.personal(mapping, form, request, response);
}
}
